package com.wmyg.antifake.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wmyg.antifake.bean.Picture;
import com.wmyg.antifake.bean.PictureConvert;
import com.wmyg.antifake.bean.Product;
import com.wmyg.antifake.dao.*;
import com.wmyg.antifake.service.ProductService;
import com.wmyg.antifake.utils.PicConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("productService")
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private FactoryMapper factoryMapper;

    @Autowired
    private QrcodeMapper qrcodeMapper;

    @Autowired
    private PictureMapper pictureMapper;


    @Override
    public int insert(Product product) {

        productMapper.insert(product);

        List<Picture>  picList = dealPicList(product,3,product.getProductImg());
        if(picList.size() != 0)
            pictureMapper.insertBatch(picList);

        List<Picture>   picList1= dealPicList(product,2,product.getProductCert());
        if(picList1.size() != 0)
            pictureMapper.insertBatch(picList1);


        List<Picture> picList2 = dealPicList(product,1,product.getLogo());
        if(picList2.size() != 0)
            pictureMapper.insertBatch(picList2);
        return 1;
    }


    List<Picture> dealPicList(Product product,int picType,String img){
        List<Picture> picList=new ArrayList();
        if(StringUtils.isNotEmpty(img)){
            String[] imgArr=img.split(",");
            for(int i=0;i<imgArr.length;i++){
                if(StringUtils.isEmpty(imgArr[i])){
                    continue;
                }
                String [] newArr=imgArr[i].split("&");
                Picture picture=new Picture(null,3,product.getProductId(),picType,newArr[0],NumberUtils.toInt(newArr[1]),NumberUtils.toInt(newArr[2]));
                picList.add(picture);
            }
        }
        return picList;
    }

    @Override
    public int batchInsertProducts(List<Product> products) {
        return productMapper.batchInsertProducts(products);
    }

    @Override
    public Product selectById(String productId) {
        Product product = productMapper.selectById(productId);
        if(product != null){
            product.setBrand(brandMapper.view(product.getBrandId()));
            product.setFactory(factoryMapper.view(product.getFactoryId()));
        }else {
            product = new Product();
        }

        List<Picture> picList = pictureMapper.selectPic(3,productId,null);
        PictureConvert pictureConvert=PicConvertUtils.getPictureConvert(picList);
        product.setImgList(pictureConvert.getPic());
        product.setLogoList(pictureConvert.getLogo());
        product.setCertList(pictureConvert.getCert());

        return product;
    }

    @Override
    public int update(Product product) {

        List<Picture>  picList = dealPicList(product,3,product.getProductImg());
        if(picList.size() != 0)
            pictureMapper.insertBatch(picList);

        List<Picture>   picList1= dealPicList(product,2,product.getProductCert());
        if(picList1.size() != 0)
            pictureMapper.insertBatch(picList1);


        List<Picture> picList2 = dealPicList(product,1,product.getLogo());
        if(picList2.size() != 0)
            pictureMapper.insertBatch(picList2);

        return productMapper.update(product);
    }

    @Override
    public PageInfo findAll(String productName, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize, "add_time desc");
        List<Product> productList = productMapper.findAll(productName);
        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        return pageInfo;
    }

    @Override
    public List<Product> findAllProduct() {
        List<Product> productList = productMapper.findAllProduct();
        return productList;
    }

    @Override
    public Map<String, BigDecimal> findQrCodeNum(Long qrCodeId) {
        return qrcodeMapper.findQrCodeNum(qrCodeId);
    }

    @Override
    public int delete(String productId){
        return productMapper.delete(productId);
    }
}
